<?php

namespace addons\thirdwork;

use app\common\library\Menu;
use think\Addons;
use think\Request;
use think\Cookie;
use think\Session;
use think\Config;
use think\Db;

/**
 * 插件
 */
class Thirdwork extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu = [
            [
                'name'    => 'thirdwork',
                'title'   => '企业第三方登录管理',
                'icon'    => 'fa fa-twitter',
                'sublist' => [
                    [
                        "name"  => "thirdwork/index",
                        "title" => "查看"
                    ],
                    [
                        "name"  => "thirdwork/del",
                        "title" => "解绑/删除"
                    ]
                ]
            ]
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('thirdwork');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable('thirdwork');
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('thirdwork');
        return true;
    }

    /**
     * 会员中心边栏后
     * @return mixed
     * @throws \Exception
     */
    public function userSidenavAfter()
    {
        $config = $this->getConfig();

        $userid = Cookie::get('uid');
        $hasBind = Db::name('thirdwork')->where('platform', $config['platform'])->where('user_id', $userid)->find();
        if (!$hasBind) {
            return false;
        }

        if ($config['platform'] === '0') {
            return false;
        }
        $request = Request::instance();
        $controllername = strtolower($request->controller());
        $actionname = strtolower($request->action());

        $platformname = '';
        switch ($config['platform']) {
            case 'dingtalk':
                $platformname = '钉钉';
                break;
            case 'wechatwork':
                $platformname = '企业微信';
                break;
            case 'feishu':
                $platformname = '飞书';
                break;
            default:
                break;
        }
        $data = [
            'controllername' => $controllername,
            'actionname'     => $actionname,
            'platformname'   => $platformname,
            'platform'       => $config['platform']
        ];
        return $this->fetch('view/hook/user_sidenav_after', $data);
    }

    /**
     *  动态配置值，供JS读取
     * @param $params
     */
    public function configInit(&$params)
    {
        $request = Request::instance();
        $config = $this->getConfig();
        $params['thirdwork'] = [
            'platform'       => $config['platform'],
            'bindaccount'    => $config['bindaccount'],
            'registerverify' => $config['registerverify'],
            'connecturl'     => $request->root() . '/thirdwork/connect/' . $config['platform'] . '.html',
            'btnimage'       => cdnurl('/assets/addons/thirdwork/img/') . $config['platform'] . '.png'
        ];
    }

    /**
     * 开启注册验证时，禁止前台直接提交注册表单
     * @return mixed
     */
    public function actionBegin()
    {
        $request = Request::instance();
        $config = $this->getConfig();
        $userinfo = Session::get($config['platform'] . '-userinfo');

        if ($config['registerverify'] === '1' && $config['platform'] !== '0' && !$userinfo && stripos($request->path(), 'user/register') && $request->isAjax() && $request->isPost()) {
            echo('{"code":0,"msg":"请扫码登录第三方平台后再注册","data":"","url":"","wait":3}');
            exit;
        }
    }
}
